package com.flash.dailyreport.service.handler.news;

import com.flash.dailyreport.entity.NewsEntity;
import com.flash.dailyreport.service.handler.type.TypeHandler;
import com.flash.dailyreport.util.HttpClientUtils;
import com.flash.dailyreport.util.HttpTextResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;

import javax.swing.text.html.parser.TagElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

/**
 * 喜马拉雅新闻处理
 *
 * @author sunyiming
 * @date 2018/9/25
 */
public class XimalayaNewsHandler implements NewsHandler {

    HttpClientUtils client = HttpClientUtils.getInstance();

    @Override
    public Map<String, List<NewsEntity>> getMainNews() {

        Map<String, String> sources = new HashMap<>();
        sources.put("滚动头条", "https://www.ximalaya.com/toutiao/6108936/");
        sources.put("大嘴说新闻", "https://www.ximalaya.com/toutiao/14522060/");
        sources.put("陆家嘴财经早餐", "https://www.ximalaya.com/toutiao/4308484/");
        sources.put("新闻早餐", "https://www.ximalaya.com/toutiao/4519297/");
        sources.put("央广新闻", "https://www.ximalaya.com/toutiao/11610698/");

        return generateNewsMap(sources);

    }

    private Map<String,List<NewsEntity>> generateNewsMap(Map<String,String> sources) {
        Map<String, List<NewsEntity>> map = new HashMap<>();
        for (String source : sources.keySet()) {
            String url = sources.get(source);
            List<NewsEntity> list = doGetMainNews(url, source);
            map.put(source, list);
        }
        return map;
    }


    private List<NewsEntity> doGetMainNews(String url, String source) {
        List<NewsEntity> list = new ArrayList<>();
        HttpTextResponse httpTextResponse = client.doHttpGet(url, null);
        String html = httpTextResponse.getResponseBody();
        Document document = Jsoup.parse(html);
        Elements elements = document.getElementsByClass("dOi2 sound-list");
        Element soundListElement = elements.get(0);
        Elements lis = soundListElement.getElementsByTag("li");
        for (Element li : lis) {
            if (!"dOi2".equals(li.className())) {
                continue;
            }
            try {
                Element textE = li.getElementsByClass("dOi2 text").first();
                Element operateE = li.getElementsByClass("dOi2 operate").first();
                Element timeE = li.getElementsByClass("dOi2 time").first();
                // 获取标题
                String title = textE.childNode(0).attr("title");
                // 获取类型
                String type = new TypeHandler().getTypeFromTitle(title);
                // 获取时间
                String time = timeE.ownText();
                // 获取链接
                String href = textE.childNode(0).attr("href");
                // 获取热度
                String count = operateE.getElementsByClass("dOi2 count").first().ownText();

                // 封装对象
                NewsEntity newsEntity = new NewsEntity(type, title, time, href, count, source);
                list.add(newsEntity);
            } catch (Throwable e) {
                System.out.println(e.getMessage());
                continue;
            }

        }


        return list;
    }

}
